package com.hxzy.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.enums.AckCode;
import com.hxzy.dto.AdminLoginDTO;
import com.hxzy.entity.MallAdminUser;
import com.hxzy.exception.ServiceException;
import com.hxzy.mapper.MallAdminUserMapper;
import com.hxzy.service.MallAdminUserService;
import com.hxzy.service.SysMenuService;
import com.hxzy.service.SysRoleService;
import com.hxzy.vo.AdminLoginVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 后台管理员列表
 */
@Log4j2
@Service
public class MallAdminUserServiceImpl extends ServiceImpl<MallAdminUserMapper, MallAdminUser> implements MallAdminUserService {



    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public AdminLoginVO login(AdminLoginDTO adminLoginDTO) {

        //1、创建spring security给我们提供的固定的接口
        UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(adminLoginDTO.getLoginName(),adminLoginDTO.getPassword());

        //2、开始登录流程
        Authentication  authenticate=this.authenticationManager.authenticate(authenticationToken);

        //3、判断是否认证成功
        if( authenticate.isAuthenticated()){
            //该用户拥有的权限
            Collection<? extends GrantedAuthority> authorities = authenticate.getAuthorities();
            //得到当前用户
            AdminLoginVO currentUser= (AdminLoginVO) authenticate.getPrincipal();
            return currentUser;
        }

       return null;



//        MallAdminUser dbUser=this.baseMapper.findByLoginName(adminLoginDTO.getLoginName());
//        if(dbUser==null){
//            log.error(adminLoginDTO.getLoginName()+","+AckCode.USER_NOT_FOUND.getMsg());
//            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
//        }
//        //密码判断
//        if(!BCrypt.checkpw(adminLoginDTO.getPassword(),dbUser.getLoginPassword())){
//            log.error(adminLoginDTO.getLoginName()+","+AckCode.LOGIN_PASSWORD_ERROR.getMsg());
//            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
//        }
//        //账号状态
//        if(dbUser.getLocked().equals("1")){
//            log.error(adminLoginDTO.getLoginName()+","+AckCode.DEVICE_BANNED.getMsg());
//            throw new ServiceException(AckCode.DEVICE_BANNED);
//        }
//
//        //生成一个自己的对象
//        AdminLoginVO  adminLoginVO=new AdminLoginVO();
//        BeanUtils.copyProperties(dbUser, adminLoginVO);
//
//        //查询该用户拥有角色
//        Set<String> ownRoleSet=this.sysRoleService.findRoleKeyByAdminId(dbUser.getAdminId());
//
//        //查询该用户拥有菜单有哪些
//        Set<String> permissSet=new HashSet<>();
//
//        //超级管理员,拥有所有权限
//        if(ownRoleSet.contains("admin")){
//            permissSet.add("*:*:*");
//        }else{
//            // 普通 角色 查询它拥有的权限
//            permissSet=this.sysMenuService.findMenuPermsByAdminId(dbUser.getAdminId());
//        }
//
//        adminLoginVO.setRoleSet(ownRoleSet);
//        adminLoginVO.setPermissionSet(permissSet);
//        return adminLoginVO;
    }

    /**
     * 根据用户名查询数据库值
     * @param username
     * @return
     */
    @Override
    public MallAdminUser findByLoginName(String username) {
        return this.baseMapper.findByLoginName(username);
    }
}
